package vip.xiaonuo.biz.modular.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.apilog.entity.DevApiLog;
import vip.xiaonuo.biz.modular.apilog.mapper.DevApiGoodsMapper;
import vip.xiaonuo.biz.modular.cabinetdoor.vo.BizCabinetDoorResp;
import vip.xiaonuo.biz.modular.event.entity.BizEvent;
import vip.xiaonuo.biz.modular.event.entity.BizEventConfig;
import vip.xiaonuo.biz.modular.event.mapper.BizEventConfigMapper;
import vip.xiaonuo.biz.modular.event.mapper.BizEventMapper;
import vip.xiaonuo.biz.modular.event.service.BizEventInvService;
import vip.xiaonuo.biz.modular.event.service.BizEventReplaceService;
import vip.xiaonuo.biz.modular.event.util.EventActionUtil;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 墨塵
 * @since 2022/12/2
 */
@Service
@Transactional
public class BizEventInvServiceImpl implements BizEventInvService {

    @Resource
    private BizEventConfigMapper bizEventConfigMapper;

    @Resource
    private BizEventMapper bizEventMapper;

    @Resource
    private BizGoodsMapper bizGoodsMapper;

    @Resource
    private BizEventReplaceService bizEventReplaceService;

    @Resource
    private DevApiGoodsMapper devApiGoodsMapper;

    @Override
    public void countInCabGoods(DevApiLog apiLog) {
        //默认信息-操作人名称
        String userName = null;
        //查询对应配置
        BizEventConfig eventConfig = null;
        if (apiLog.getReqType() == 3) {
            //手动盘库
            eventConfig = bizEventConfigMapper.selectById("14");
            userName = bizEventReplaceService.getUserName(apiLog.getUserId());
        } else {
            //自动盘库
            eventConfig = bizEventConfigMapper.selectById("15");
            userName = "系统";
        }
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        eventDesc = eventDesc.replace("{{user}}", userName);
        //柜门名称
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        //试剂处理
        if (cabinetDoorResp.getCabinetDoorType() == 1) {
            eventDesc = eventDesc.replaceAll("物品", "试剂");
            eventDesc = eventDesc.replaceAll("维修中物品\\{\\{rep-num\\}\\}件,", "");
        }
        eventDesc = eventDesc.replace("{{cab-door}}", userName);
        //获取数量集合
        Map<String, String> dimCountMap = devApiGoodsMapper.getDimCount(apiLog.getId());
//        全部物品：就是柜子里本来应该有多少物品，除去报废且离柜的
//        在柜：当前在柜子里的物品，不要管状态
//        维修中：就是状态是维修中的物品，不分在柜还是离柜
//        已报废：状态是报废，并且在柜的物品
//        离柜：所有离柜的物品，除去报废且离柜的
        eventDesc = eventDesc.replace("{{total-num}}", MapUtil.getStr(dimCountMap, "totalNum", "0"));
        eventDesc = eventDesc.replace("{{itc-num}}", MapUtil.getStr(dimCountMap, "inCabNum", "0"));
        eventDesc = eventDesc.replace("{{ooc-num}}", MapUtil.getStr(dimCountMap, "offCabNum", "0"));
        eventDesc = eventDesc.replace("{{rep-num}}", MapUtil.getStr(dimCountMap, "repNum", "0"));
        eventDesc = eventDesc.replace("{{scrap-num}}", MapUtil.getStr(dimCountMap, "scrapNum", "0"));
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setEventUserId(apiLog.getUserId());
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);

        //【邮件】柜门管理员
        bizEventReplaceService.sendEmailForDoorAdmin(event);
    }

    @Override
    public void checkInCabScraoGoods(DevApiLog apiLog) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("24");
        //获取在柜的报废物品
        List<BizGoods> goodsList = devApiGoodsMapper.getScrapList(apiLog.getId());
        for (BizGoods goods : goodsList) {
            //描述
            String eventDesc = eventConfig.getEventDesc();
            //物品信息
            String goodsName = bizEventReplaceService.getGoodsNameForEntity(goods);
            eventDesc = eventDesc.replace("{{goods}}", goodsName);
            //柜门名称
            String doorName = bizEventReplaceService.getCbaDoorNameForId(goods.getCurCabinetId(), goods.getCurCabinetDoorId());
            BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForId(goods.getCurCabinetId(), goods.getCurCabinetDoorId());
            eventDesc = eventDesc.replace("{{cab-door}}", doorName);
            //事件时间
            eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
            //事件动作
            eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
            //操作人
            eventDesc = eventDesc.replace("{{user}}", bizEventReplaceService.getUserName(apiLog.getUserId()));
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setApiLogId(apiLog.getId());
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(apiLog.getEventTime());
            event.setEventDesc(eventDesc);
            event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
            event.setEventUserId(apiLog.getUserId());
            event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
            event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
            event.setCabinetDoorId(cabinetDoorResp.getId());
            event.setGoodsId(goods.getId());
            bizEventReplaceService.clearBizEvent(event);
            //类目切换
            bizEventReplaceService.itemSwitch(event,goods.getGoodsType());
            bizEventMapper.insert(event);

            //【邮件】柜门管理员
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }
    }
}
